<?php
class ClassaddAction extends Action{
	
	public function main(){
		$this->_request['action'] = isset($this->_request['action']) ? $this->_request['action'] : 'default';
		switch ($this->_request['action']) {
			case 'modify':
				$this->__modify();
				break;
			default:
				$this->__load();
				break;
		}

	}

	private function __load(){
		$row = array();
		$ChannelIDarr = array(0);
		if (isset($this->_request['ClassID'])) {
			$ClassID = $this->_request['ClassID'];
			$ClassificationDAO = new  ClassificationDAO();
			$row =$ClassificationDAO->getOneByID($ClassID);

			
			foreach ($row as $key => $value) {
				$this->_response[$key] = $value;
			}
			$ChannelIDarr = explode(",", $this->_response['ChannelID']);
			//debug($this->_response);
		}
		//debug($this->_response);
		$selected = new Selected();
		$channelobj = new  ChannelInfoDAO();
		$channellist = $channelobj->getList();
		$this->_response['ChannelID'] = $selected->getCustomize($channellist,"ChannelID","ChannelName",$ChannelIDarr);
		//debug($channellist);
		
		$this->_response['ClassDiaplay'] = $selected->get("ClassDiaplay",$this->_response['ClassDiaplay']);
		$this->_response['ClassDiaplayOrder'] = $selected->get("ClassDiaplayOrder",$this->_response['ClassDiaplayOrder']);
		$this->_response['ClassType'] = $selected->get("ClassType",$this->_response['ClassType']);
		//debug ($this->_response);
		//$this->_response['default-row'] = $row;
	}


	private function __modify(){
		$inputarr =  array();
		$inputarr['ClassName'] = $this->_request['ClassName'];
		$inputarr['ClassDiaplay'] = $this->_request['ClassDiaplay'];
		$inputarr['ClassDiaplayOrder'] = $this->_request['ClassDiaplayOrder'];
		$inputarr['ClassType'] = $this->_request['ClassType'];
		//$inputarr['ChannelID'] = $this->_request['ChannelID'];
		$inputarr['Comment'] = $this->_request['Comment'];
		$ClassID = (int)$this->_request['ClassID']?(int)$this->_request['ClassID'] : 0;
		$fileupload = new Fileupload(FILE_UPLOAD_PATH.SLASH.'class');
		//debug($_FILES["ClassIcon1"]);
		if ($_FILES["ClassIcon1"]['tmp_name']) {
			$fileupload->run("ClassIcon1",1);
			$inputarr['ClassIcon1'] = FILE_UPLOAD_PATH.SLASH.'class'.SLASH.$fileupload->saveName;
		}
		if ($_FILES["ClassIcon2"]['tmp_name']) {
			$fileupload->run("ClassIcon2",1);
			$inputarr['ClassIcon2'] = FILE_UPLOAD_PATH.SLASH.'class'.SLASH.$fileupload->saveName;
		}
		$ClassificationDAO = new  ClassificationDAO();
		if ($ClassID){
			
			$ret = $ClassificationDAO->modify($ClassID,$inputarr);
		}else {
			
			$ret = $ClassificationDAO->add($inputarr);
		}
		//debug($this->_request);
		//die();
		//debug($ret);
		//$ClassName = $this->_request['ClassName'];
		header("Location:index.php?do=Class/class_list");

		die();
		//debug($this->_request);
	}




}